A class is a blueprint for objects. It can have properties (fields), methods, and constructors. Classes support encapsulation, inheritance, and polymorphism.
class Student {
// Properties (instance variables)
String name;
int age;
// Constructor
Student(String name, int age) {
this.name = name;
this.age = age;
}
// Method to display details
void displayDetails() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Access modifiers define the scope of a class, method, or variable. Common types include:
class Car {
public String model; // Accessible from anywhere
private int year; // Accessible only within this class
public void setYear(int year) {
this.year = year; // Private variable accessed via method
}
public int getYear() {
return year;
}
}
Method overloading allows multiple methods with the same name but different parameters. This enhances readability and usability.
class Calculator {
// Method with two parameters
int add(int a, int b) {
return a + b;
}
// Overloaded method with three parameters
int add(int a, int b, int c) {
return a + b + c;
}
}
Garbage collection automatically deallocates memory for objects that are no longer referenced. Java’s garbage collector helps improve memory management.
class GarbageExample {
protected void finalize() { // Called by Garbage Collector before object destruction
System.out.println("Object is garbage collected");
}
}
'this' refers to the current object instance. It distinguishes instance variables from method parameters.
class Person {
String name;
Person(String name) {
this.name = name; // Refers to the instance variable
}
}
The 'static' keyword allows variables, methods, and blocks to belong to the class rather than an instance. Static members are loaded only once at class loading time.
class MathUtils {
static int square(int num) { // Static method
return num * num;
}
static { // Static block
System.out.println("Static block executed");
}
}
The 'final' keyword is used to declare constants, prevent method overriding, or inheritance. Final variables cannot be reassigned, final methods cannot be overridden, and final classes cannot be extended.
class FinalExample {
final int CONSTANT = 100; // Constant variable
final void display() { // Final method (cannot be overridden)
System.out.println("Final method");
}
}
Wrapper classes convert primitive data types into objects. This helps in object manipulation and data conversion.
class WrapperDemo {
public static void main(String[] args) {
int a = 10;
Integer obj = Integer.valueOf(a); // Boxing
int b = obj.intValue(); // Unboxing
}
}
The String class provides various methods for string manipulation. Strings are immutable in Java, meaning their content cannot be changed after creation.
class StringDemo {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.length()); // Length of string
System.out.println(str.toUpperCase()); // Convert to uppercase
System.out.println(str.replace("World", "Java")); // Replace text
}
}